package com.atguigu.amqp.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : leimingchao
 * @date Date : 2021年12月18日 11:28
 */
@Configuration
public class RabbitDeadDelayConfig {

    /**
     * 一个普通交换机
     * @return
     */
    @Bean
    public Exchange exchange() {
        return new DirectExchange("user.order.delay.exchange",true,false);
    }

    @Bean
    public Queue delayQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //这个队列里面所有消息过期时间,以毫秒为单位表示TTL的值，100秒
        arguments.put("x-message-ttl",100 * 1000);
        //消息过期交给哪个交换机
        arguments.put("x-dead-letter-exchange","user.order.exchange");
        //消息过期后发给哪个路由键
        arguments.put("x-dead-letter-routing-key","order");
        return new Queue("user.order.delay.queue",true,false,false,arguments);
    }

    @Bean
    public Binding delayBinding() {
        return new Binding("user.order.delay.queue",
                Binding.DestinationType.QUEUE,
                "user.order.delay.exchange",
                "order_delay",null);
    }

    @Bean
    public Exchange deadExchange() {
        return new DirectExchange("user.order.exchange",true,false);
    }

    @Bean
    public Queue deadQueue() {
        return new Queue("user.order.queue",true,false,false,null);
    }

    @Bean
    public Binding deadBinding() {
        return new Binding("user.order.queue",
                Binding.DestinationType.QUEUE,
                "user.order.exchange",
                "order",null);
    }
}
